<template>
  <section class="main">
    <input
      id="toggle-all"
      class="toggle-all"
      type="checkbox"
      v-model="ischecked"
    />
    <label for="toggle-all">Mark all as complete</label>
    <ul class="todo-list">
      <!-- 当任务已完成，可以给 li 加上 completed 类，会让元素加上删除线 -->
      <li
        :class="{ completed: item.isDone }"
        v-for="item in list"
        :key="item.id"
      >
        <div class="view">
          <input
            class="toggle"
            type="checkbox"
            :checked="item.isDone"
            @change="change(item.id)"
          />
          <label>{{ item.name }}</label>
          <button class="destroy" @click="del(item.id)"></button>
        </div>
      </li>
      <!-- <li>
        <div class="view">
          <input class="toggle" type="checkbox" />
          <label>行万里路</label>
          <button class="destroy"></button>
        </div>
      </li> -->
    </ul>
  </section>
</template>
<script>
import { mapState } from 'vuex'
export default {
  props: {
    list1: {
      type: Array,
      require: true,
    },
    type: {
      type: String,
    },
    
  },
  methods: {
    del(id) {
      // console.log(id)
      this.$emit('del', id)
    },
    change(id) {
      this.$emit('change', id)
    },
  },
  created() {
    this.$store.dispatch('cart/getList')
  },

  computed: {
    ...mapState('cart',['list']),
    showList() {
      if (this.type === 'completed') {
        // 显示已完成
        return this.list.filter((item) => item.isDone === true)
      } else if (this.type === 'active') {
        // 显示未完成
        return this.list.filter((item) => item.isDone === false)
      } else {
        return this.list // 全部显示
      }
    },
    ischecked: {
      get() {
        return this.list.every((item) => item.isDone)
      },
      set(e) {
        this.$emit('checkAll', e)
      },
    },
  },
}
</script>
